﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DIS.Infrastructure.Entities.Entity;
using DIS.Tool.Framework;
using System.Web.Security;
using System.Web;
using DIS.Infrastructure.Entities;
using System.Data.SqlClient;
using System.Data;

namespace DIS.Portal.Biz
{
    public class HotelFeeBiz
    {
        public static ItemListBox<HotelFee> GetAllHotelFee(int page = 0, int pageSize = 20)
        {
            string username = System.Web.HttpContext.Current.User.Identity.Name;
            var count = new HotelFee().Count(c => c.UserName == username);
            using (Context ctx = new Context())
            {
                var data = (from c in ctx.HotelFee
                            join d in ctx.User
                            on c.UserName equals d.UserName
                            join f in ctx.Project
                            on c.ProjectId equals f.Id
                            where c.UserName == username
                            orderby c.Year, c.Month descending
                            select new { c, d,f })
                          .ToList()
                          .Select(e => new HotelFee().Set(e.c).LoadUser(e.d).LoadProject(e.f)).ToList();

                return new ItemListBox<HotelFee>(data).BuildPage(
                  count, page, 20, new PageParameter() { Style = "workspace" });
            }
        }

        public static HotelFee SaveHotelFee(HotelFee hotelFee, List<HotelFeeDetail> detailList)
        {
            if (hotelFee.Id > 0)
            {
                var hotelFeeTemp = new HotelFee().Find(hotelFee.Id.ToString());
                hotelFeeTemp.Year = hotelFee.Year;
                hotelFeeTemp.Month = hotelFee.Month;
                hotelFeeTemp.StartDate = hotelFee.StartDate;
                hotelFeeTemp.EndDate = hotelFee.EndDate;
                hotelFeeTemp.TotalDays = hotelFee.TotalDays;
                hotelFeeTemp.TotalFee = hotelFee.TotalFee;
                hotelFeeTemp.InvoiceCount = hotelFee.InvoiceCount;
                hotelFeeTemp.ProjectId = hotelFee.ProjectId;
                hotelFeeTemp.PostModify();
            }
            else
            {
                hotelFee = hotelFee.PostAdd();
            }
            foreach (var item in detailList)
            {
                item.HotelId = hotelFee.Id;
                item.PostAdd();
            }
            return hotelFee;
        }
    }
}
